#include "ul_log_adapter.h"
#include <cstdio>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include "ul_define.h"
using namespace std;
using namespace LinuxNetworkUtility;
namespace
{
  UlBasicLogAdapter g_ul_basic_log_adapter;
  const char * const ul_log_level_identifier[]=
  {
    "[DEBUG] ",
    "[INFO] ",
    "[NOTICE] ",
    "[MONITOR] ",
    "[WARNING] ",
    "[ERROR] "
  };
}
namespace LinuxNetworkUtility
{
  UlLogAdapter *g_ul_log_adapter = &g_ul_basic_log_adapter;
}


int LinuxNetworkUtility::UlBasicLogAdapter::write_log(const ul_log_level_t level, const char *fmt, ...)
{
  int err = 0;
  va_list args;
  int _level = static_cast<int>(level);
  if (NULL == fmt)
  {
    UL_LOG_WARNING("argument error [fmt:%p]", fmt);
    err = EINVAL;
  }
  if (0 == err && (_level < DEBUG || _level > ERROR))
  {
    UL_LOG_WARNING("invalid log level [level:%d]", _level);
    _level = WARNING;
  }
  if (0 == err)
  {
    va_start(args, fmt);
    vfprintf(stderr, fmt,args);
    va_end(args);
  }
  errno = err;
  return ERRNO2ERR(err);
}

void LinuxNetworkUtility::set_ul_log_adaptor(UlLogAdapter * log_adapter)
{
  if (NULL == log_adapter)
  {
    g_ul_log_adapter = &g_ul_basic_log_adapter;
  }
  else
  {
    g_ul_log_adapter = log_adapter;
  }
}
